package com.potenza.controller;

import com.potenza.mapper.ProductAccessMapper;
import com.potenza.model.ProductAccessLog;
import org.apache.calcite.avatica.AvaticaConnection;
import org.apache.calcite.avatica.AvaticaStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("druid")
public class ProductAccessController {

    @Autowired
    ProductAccessMapper productAccessMapper;

    @GetMapping("/productaccess/findAll")
    public List<ProductAccessLog> findAll(){
        return productAccessMapper.getProductAccessLogList();
    }

    @GetMapping("/productaccess/findAll1")
    public void findAll1() throws SQLException {
        String url = "jdbc:avatica:remote:url=http://192.168.83.137:8082/druid/v2/sql/avatica/";
        AvaticaConnection connection = (AvaticaConnection) DriverManager.getConnection(url);

        //SQL语句,查询2020-4-10 11:50:30之后的访问uri和访问数量
        String sql="SELECT * FROM \"product_access\"";

        //创建Statment
        AvaticaStatement statement = connection.createStatement();

        //执行查询
        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()) {
            //获取uri
            String uri = resultSet.getString("uri");
            String __time = resultSet.getString("__time");
            System.out.println(uri+"--------->"+__time);
        }
    }
}
